<template>
    <div class="about">
        <el-form :inline="true" :model="quertInfo" class="demo-form-inline">
            <el-form-item label="评价内容">
                <el-input v-model="quertInfo.marktext" placeholder="请输入评价内容" clearable style="width: 240px" />
            </el-form-item>
            <el-form-item label="评价星级">
                <el-input v-model="quertInfo.markstar" placeholder="请输入星级" clearable style="width: 240px" />
            </el-form-item>
            <br>
            <el-form-item label="评价用户">
                <el-select v-model="selectUserName" placeholder="请选择要查询的用户" clearable style="width: 240px">
                    <el-option v-for="item in userList" :key="item.id" :label="item.nickName" :value="item.nickName" />
                </el-select>
            </el-form-item>

            <el-form-item label="咨询老师">
                <el-select v-model="selectConsultName" placeholder="请选择要查询的咨询师" clearable style="width: 240px">
                    <el-option v-for="item in consultList" :key="item.id" :label="item.name" :value="item.name" />
                </el-select>
            </el-form-item>
            <br>
            <el-form-item>
                <el-button type="primary" @click="getArticleList2">搜索</el-button>
            </el-form-item>
        </el-form>
        <div class="mb20">
            <!-- <el-button @click="toAdd" type="primary" icon="Plus" plain>新增</el-button> -->
            <el-button @click="toScopeDelete" type="danger" icon="Delete" plain>删除</el-button>
        </div>
        <el-table :data="tableData" style="width: 100%" row-key="id" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column fixed prop="id" label="编号" width="90" />
            <el-table-column prop="marktext" label="评价内容" width="150" />
            <el-table-column prop="markstar" label="评价星级" width="150" >
                <template #default="scope">
                    <el-rate
                    v-model="scope.row.markstar"
                    disabled
                    show-score
                    text-color="#ff9900"
                    :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
                    />
                </template>
            </el-table-column>
            <el-table-column prop="createtime" label="创建时间" width="150" show-overflow-tooltip />
            <el-table-column prop="user.nickName" label="评价用户" width="150" />
            <el-table-column prop="consult.name" label="被评价咨询师" width="150" />
            <el-table-column fixed="right" label="操作" min-width="120">
                <template #default="scope">
                    <el-button link type="primary" size="small" @click="toEdit(scope.row)">修改</el-button>
                    <el-button link type="primary" @click="onDel(scope.row.id)" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-model:current-page="quertInfo.pageNum" v-model:page-size="quertInfo.pageSize"
            :page-sizes="[1, 5, 10, 20]" :size="size" :disabled="disabled" :background="false"
            layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" style="margin-top: 20px;" />
    </div>
    <!-- 添加，编辑弹框 -->
    <!-- <el-dialog v-model="showPop" :title="isEdit ? '编辑' : '添加'">
        <DicAddCom v-if="showPop" @onCancel="onCancel" :dicid="dicid" :isEdit="isEdit" />
    </el-dialog> -->
    <el-dialog v-model="saveOrUpdateVisible" :title="saveOrUpdateTitle" width="500">
        <el-form :model="article">
            <!-- <el-form-item label="编号" :label-width="formLabelWidth">
                <el-input v-model="article.id" autocomplete="off" />
            </el-form-item> -->

            <el-form-item label="评价内容" :label-width="formLabelWidth">
                <el-input v-model="article.marktext" autocomplete="off" />
            </el-form-item>

            <el-form-item label="评价星级" :label-width="formLabelWidth">
                <el-input v-model="article.markstar" autocomplete="off" />
            </el-form-item>



        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="saveOrUpdate(row)">
                    {{ saveOrUpdateTitle }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script setup>
import { reactive, ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

//变量区域开始

//
const userList = ref();
const consultList = ref();
const selectUserName = ref();
const selectConsultName = ref();

//批量删除
const selectedIds = ref([]);
const handleSelectionChange = (val) => {
    selectedIds.value = val.map(item => item.id);
}

const quertInfo = ref({
    pageNum: 1,
    pageSize: 5,
    marktext: '',
    markstar: '',
    consultId: '',
    userId: '',
})

const total = ref(0)

// 修改或添加
const saveOrUpdateVisible = ref(false);
const article = ref({
    id: 0,
    marktext: '',
    markstar: '',
    createtime: '',
    consult_id: '',
    user_id: '',
    user: {
        id: 0,
        nickname: '',
    },
    consult: {
        id: 0,
        name: '',
    }
})
const saveOrUpdateTitle = ref('添加');

const imageUrl = ref('')
//变量区域结束

//函数区域开始


//  取消修改或保存
const close = () => {
    saveOrUpdateVisible.value = false;
    imageUrl.value = '';
    article.value = {
        id: 0,
        name: '',
        publicitytext: '',
        createtime: '',
    }
}

const toScopeDelete = () => {
    if (selectedIds.value.length == 0) {
        ElMessage({
            message: '请选择要删除的记录！',
            type: 'error',
        })
        return;
    } else {
        ElMessageBox.confirm(
            '真的要删除吗？',
            '提示',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(() => {
                //调用后端删除接口
                axios.delete('/usermark/deleteByIds',
                    {
                        data: selectedIds.value
                    }
                )
                    .then(res => {
                        let result = res.data;
                        if (result.code != 0) {
                            ElMessage({
                                message: '删除成功！',
                                type: 'success',
                            })
                            getcoursetypeList();

                        } else {
                            ElMessage({
                                message: '删除失败！',
                                type: 'error',
                            })
                            return;
                        }
                    })
            })
    }
}

//  保存或更新
const saveOrUpdate = () => {
    if (saveOrUpdateTitle.value == '添加') {
        console.log("添加")
        // 调用后端接口添加
        // 
        axios.post('/publicity', article.value)
            .then(resp => {
                // console.log(resp.data)
                let result = resp.data;  // resp是promise对象，.data返回我们自己的标准R
                if (result.code == 1) {
                    getArticleList();
                } else {
                    console.log('添加文章失败')
                }
            })
    } else {
        // 调用后端接口修改
        axios.put('/usermark', article.value)
            .then(resp => {
                // console.log(resp.data)
                let result = resp.data;  // resp是promise对象，.data返回我们自己的标准R
                if (result.code == 1) {
                    // 重新获取列表
                    getArticleList();
                } else {
                    console.log('修改文章失败')
                }
            })
    }
    // 关闭弹框
    saveOrUpdateVisible.value = false;
    imageUrl.value = '';
    article.value = {
        id: 0,
        name: '',
        publicitytext: '',
        createtime: '',
    }
}

//  去修改的页面
const toEdit = row => {
    saveOrUpdateVisible.value = true;
    saveOrUpdateTitle.value = '编辑';
    article.value = row;

    console.log('article', article.value)
}

//  去添加的页面
const toAdd = row => {
    saveOrUpdateVisible.value = true;
    saveOrUpdateTitle.value = '添加';
    article.value = {
        id: 0,
        name: '',
        publicityText: '',
        createTime: '',
    }
}
//条件查询
const getArticleList2=()=>{
    const userNickName = userList.value.find(aaa => aaa.nickName === selectUserName.value);
    const consultName = consultList.value.find(aaa => aaa.name === selectConsultName.value);
    // console.log("selectUserName",selectUserName.value)
    // console.log("userNickName",userNickName)
    if (userNickName) {
        quertInfo.value.userId = userNickName.id;
    } else {
        quertInfo.value.userId = '';
    }
    if (consultName) {
        quertInfo.value.consultId = consultName.id; 
    } else {
        quertInfo.value.consultId = ''; 
    }
    // console.log("userNickName.id",userNickName.id)
    // console.log("quertInfo.user_id",quertInfo.user_id)
    // const uid = userNickName.id
    // quertInfo.value.userId = uid
    // console.log("=========",quertInfo.value)

    //使用axios请求数据
    axios.get('/usermark/getAll', {
        params: quertInfo.value
    })
        .then(res => {
            let result = res.data;
            console.log("quertInfo===",quertInfo.value)
            if (result.code == 1) {
                tableData.value = result.data.list;
                total.value = result.data.total;
                console.log("tableData",tableData.value)
            } else {
                console.log('获取公告失败')
            }
        })
        .catch(err => {
            console.log('err', err);
        })
}

const getArticleList = () => {
    //使用axios请求数据
    axios.get('/usermark/getAll', {
        params: quertInfo.value
    })
        .then(res => {
            let result = res.data;
            if (result.code == 1) {
                tableData.value = result.data.list;
                total.value = result.data.total;
                starValue.value = result.data.list.markstar
                console.log("starValue==",starValue)
            } else {
                console.log('获取公告失败')
            }
        })
        .catch(err => {
            console.log('err', err);
        })
}
const handleSizeChange = (val) => {
    quertInfo.value.pageSize = val;
    quertInfo.value.pageNum = 1;
    getArticleList();
}
const handleCurrentChange = (val) => {
    quertInfo.value.pageNum = val;
    getArticleList();
}

//  获取用户列表
const getUserList = () => {
    axios.get('/user/user',{
        params:{
            pageNum: 1,
            pageSize: 1000,
        }
    }
        
    )
       .then(res => {
            let result = res.data;
            console.log('用户列表数据', result)
            if (result.code == 1) {
                userList.value = result.data.list;
            } else {
                console.log('获取用户列表失败')
            }
        })
       .catch(err => {
           
       })  
}
//  获取咨询师列表
const getConsultList = () => {
    axios.get('/consult2/findAll')
       .then(res => {
            let result = res.data;
            //  console.log('咨询师列表数据', result)
             consultList.value = result;
            //  console.log('咨询师列表数据====', consultList.value)
        })
       .catch(err => {
           
       })  
}
//函数区域结束

//钩子函数区域开始
onMounted(() => {
    getArticleList();
    getUserList();
    getConsultList();
})
//钩子函数区域结束

const formInline = reactive({
    name: '',
    state: '',
    date: '',
})

// 删除
const onDel = id => {
    console.log('id', id);
    ElMessageBox.confirm(
        '真的要删除吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            console.log('确定');
            //调用后端删除接口
            axios.delete('/usermark/' + id)
                .then(res => {
                    let result = res.data;  // resp是promise对象，.data返回我们自己的标准R 对象
                    if (result.code != 1) {
                        ElMessage({
                            message: '删除失败！',
                            type: 'error',
                        })
                        return;
                    } else {
                        ElMessage({
                            message: '删除成功！',
                            type: 'success',
                        })
                        getArticleList();
                    }
                })

        })
        .catch(() => { })
}

const tableData = ref([
    {
        id: 1,
        name: '颜色',
        state: 1,//1可用，0禁用
        mark: 'Los Angeles',
        date: '2016-05-03',
    },
    {
        id: 2,
        name: '性别',
        state: 0,//1可用，0禁用
        mark: 'Los Angeles',
        date: '2016-05-03',
    },
    {
        id: 3,
        name: '车牌',
        state: 1,//1可用，0禁用
        mark: 'Los Angeles',
        date: '2016-05-03',
    },
])
</script>
<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.about {
    margin-left: 20px;
}
</style>